//到货通知单
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var goods_name=thisform.find('input[name=goods_name]').val();
		var goods_no=thisform.find('input[name=goods_no]').val();
		$('#storageDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#storageDatagrid').datagrid({ 
			queryParams:{
				goods_name:goods_name,
				goods_no:goods_no
			}
		});
	})
	//商品搜索
	$('.searchgoodsBar').click(function(){
		var thisform=$(this).parent('form');
		var goods_name=thisform.find('input[name=goods_name]').val();
		var goods_no=thisform.find('input[name=goods_no]').val();
		$('#aoDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#aoDatagrid').datagrid({ 
			queryParams:{
				goods_name:goods_name,
				goods_no:goods_no
			}
		});
	})
	$('#storageDatagrid').datagrid({
		//url:getstorageUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getstorageUrl,
		title:'盘亏核减列表',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'order_no',title:'流水单号',sortable:true},
		{field:'goods_order_no',title:'商品入库单号',sortable:true},
		{field:'goods_no',title:'商品编码',sortable:true},
		{field:'goods_name',title:'商品名称',sortable:true},
		{field:'goods_nums',title:'数量',sortable:true},
		{field:'add_time',title:'添加时间',sortable:true},
		{field:'examine_time',title:'审核时间',sortable:true},
		{field:'status',title:'审核状态',sortable:true},
		{field:'mark',title:'备注',sortable:true}
		]],
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		onBeforeLoad:function(data){
			data.cate=2;
        },
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
	});
});
storageObj={
	add:function(url){//新增数据
		var Row=$('#aoDatagrid').datagrid('getSelections');
        if (Row.length>0){
        	$.messager.confirm('提示','真的要添加这'+Row.length+'个信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<Row.length;i++){
                		ids.push(Row[i].id);
                	}
                	var id=ids.join(',');
                    $.ajax({
						type: 'POST',
						data: {ids:id,cate:2},
						url: url,
						success:function(data){
							if(data.status==1){	//检查通过，跳转生成波次单页面
								$.messager.confirm('提示',data.message,function(r){
									$('#storageAddDialog').dialog('close');
									$('#storageDatagrid').datagrid('reload');
								});
							}else{			//检查未通过，刷新页面
								$.messager.alert('错误提示',data.message,'error');
							}
							return false;
						}
					})
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	save:function(url){//编辑后保存数据
		$('#storageEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#storageEditDialog').dialog('close');
                    });
                }else{
                    $('#storageEditDialog').dialog('close');      
                    $('#storageDatagrid').datagrid('reload');    
                }
            }
        });
	},
	saveBar:function(){//保存数据
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#storageDatagrid').datagrid('validateRow', editIndex)){
			$('#storageDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#storageDatagrid').datagrid('acceptChanges');
		}
	},
	showAll:function(){//显示默认条件下所有数据
		$('#storageDatagrid').datagrid({ queryParams:''});
	},
	showgoodsAll:function(){//显示默认条件下所有数据
		$('#aoDatagrid').datagrid({ queryParams:''});
	},
	reloadBar:function(){//刷新数据
		$('#storageDatagrid').datagrid('reload');
	},
	recycle:function(){//查看回收站数据
		$('#storageDatagrid').datagrid({ queryParams:{status:-1}});
	},
	addBar:function(){//显示添加框
		$('#storageAddDialog').dialog('open').dialog('setTitle','批次商品列表');
		$('#aoDatagrid').datagrid({
			//url:getstorageUrl+"?randnum="+Math.floor(Math.random()*1000000),
			url:getgoodsUrl,
			title:'',
			fitColumns:true,
			rownumbers:true,
			pageSize:25,
			pageNumber:1,
			pageList:[5,10,15,20,25,30,40,50],
			columns:[[
			{field:'id',title:'id',checkbox:true},
			{field:'order_no',title:'单号',sortable:true},
			{field:'goods_no',title:'商品编码',sortable:true},
			{field:'goods_name',title:'商品名称',sortable:true},
			{field:'freez_amount',title:'冻结数量',sortable:true},
			{field:'storage_amount',title:'库存',sortable:true},
			{field:'add_time',title:'添加时间',sortable:true}
			]],
			onLoadError:function(data){
				var info=eval('('+data.responseText+')');
				$.messager.confirm('错误提示',info.message,function(r){
					var tab=$('#Tabs').tabs('getSelected');
					var index=$('#Tabs').tabs('getTabIndex',tab);
					$('#Tabs').tabs('close',index);
				});
			},
			rowStyler:function(index,row){
				if (row.color==0){
					return 'background-color:pink;color:blue;font-weight:bold;';
				}
			},
			pagination:true,
			fit:true,
		});
	},
	editBar:function(){//显示编辑框
		var storageRow=$('#storageDatagrid').datagrid('getSelections');
        if (storageRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(storageRow.length==1){            	
        	$('#storageEditDialog').dialog('open').dialog('setTitle','编辑用户');
            $('#storageEditForm').form('load',storageRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	removeBar:function(){//删除数据
		var storageRow=$('#storageDatagrid').datagrid('getSelections');
        if (storageRow.length>0){
        	$.messager.confirm('删除提示','真的要删除这'+storageRow.length+'个信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<storageRow.length;i++){
                		ids.push(storageRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(storageDelUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#storageAddDialog').dialog('close');       
								$('#storageDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	clearBar:function(){//删除/清空回收站数据
		var storageRow=$('#storageDatagrid').datagrid('getSelections');
        if (storageRow.length>0){
        	$.messager.confirm('清空提示','真的要清空这'+storageRow.length+'信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<storageRow.length;i++){
                		ids.push(storageRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(storageClearUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#storageAddDialog').dialog('close');       
								$('#storageDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	undoBar:function(){//恢复回收站数据
		var storageRow=$('#storageDatagrid').datagrid('getSelections');
        if (storageRow.length>0){
        	$.messager.confirm('恢复提示','真的要恢复这'+storageRow.length+'信息吗?',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<storageRow.length;i++){
                		ids.push(storageRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(storageUndoUrl,{id:id},function(result){
                        if (result.status){
							$.messager.alert('提示信息',result.message,'info',function(r){
								$('#storageAddDialog').dialog('close');       
								$('#storageDatagrid').datagrid('reload'); 
							});
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	examineBar:function(value){//审核数据
		var storageRow=$('#storageDatagrid').datagrid('getSelections');
		if(value==1){
			var text='确定审核通过吗！';
		}
		if(value==2){
			var text='确定审核不通过吗！';
		}
        if (storageRow.length>0){
        	$.messager.confirm('审核提示',text,function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<storageRow.length;i++){
                		ids.push(storageRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(storageExamineUrl,{id:id,value:value},function(result){
                        if (result.status){
                            $('#storageDatagrid').datagrid('reload');
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
                    });	
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	}
}